package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.BaseEntity;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingLevelPageQueryDto;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.nursing.NursingPlan;
import com.zzyl.entity.nursing.NursingProject;
import com.zzyl.entity.nursing.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import lombok.AllArgsConstructor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.List;

@Service
@AllArgsConstructor
public class NursingPlanServiceImpl implements NursingPlanService {
	
	private final NursingPlanMapper nursingPlanMapper;
	private final NursingProjectPlanMapper nursingProjectPlanMapper;
	private final NursingLevelMapper nursingLevelMapper;
	private final NursingProjectMapper nursingProjectMapper;
	
	@Override
	public List<NursingPlanVo> queryAll() {
		NursingPlan nursingPlan = new NursingPlan();
		nursingPlan.setStatus(1);
		List<NursingPlan> nursingPlanList = nursingPlanMapper.selectList(nursingPlan);
		return BeanUtil.copyToList(nursingPlanList, NursingPlanVo.class);
	}
	
	@Override
	public void createNursingPlan(NursingPlanDto nursingPlanDto) {
		//1.保存护理计划Plan
		NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
		nursingPlanMapper.insertOne(nursingPlan);
		Long nursingPlanId = nursingPlan.getId();
		//2.构建护理计划ProjectPlanList
		List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
		if (CollUtil.isNotEmpty(projectPlans)) {
			projectPlans.forEach(pp -> pp.setPlanId(nursingPlanId));
			//3.批量保存护理计划ProjectPlanList
			nursingProjectPlanMapper.batchInsertList(projectPlans);
		}
	}
	
	@Override
	public PageBean<NursingPlanVo> queryNursingPlanPage(Integer pageNum, Integer pageSize, String name, Integer status) {
		//0.参数校验
		pageNum = Math.max(pageNum, 1);
		pageSize = Math.max(pageSize, 5);
		//1设置分页参数
		PageHelper.startPage(pageNum, pageSize);
		//2.根据条件查询列表
		NursingPlan nursingPlan = NursingPlan.builder().planName(name).status(status).build();
		Page<NursingPlanVo> page = nursingPlanMapper.selectPage(nursingPlan);
		//3.构建分页结果
		return PageBean.of(page);
	}
	
	@Override
	public NursingPlanVo queryNursingPlanDetail(Long id) {
		//1.查询护理计划:NursingPlan
		NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
		//2.查询关联的护理项目计划：List<NursingProjectPlan>
		List<NursingProjectPlan> nursingProjectPlanList = nursingProjectPlanMapper.selectList(NursingProjectPlan.builder().planId(id).build());
		//3.构建对象：NursingPlanVo
		NursingPlanVo nursingPlanVo = BeanUtil.copyProperties(nursingPlan, NursingPlanVo.class);
		nursingPlanVo.setNursingProjectPlanList(nursingProjectPlanList);
		return nursingPlanVo;
	}
	
	@Override
	public void updateNursingPlan(NursingPlanDto nursingPlanDto) {
		//1.修改NursingPlan
		Long id = nursingPlanDto.getId();
		Assert.notNull(id);
		NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
		nursingPlanMapper.updateById(nursingPlan);
		//2.删除NursingProjectPlan
		nursingProjectPlanMapper.deleteBy(NursingProjectPlan.builder().planId(id).build());
		//3.再批量保存
		List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
		if (CollUtil.isNotEmpty(projectPlans)) {
			nursingProjectPlanMapper.batchInsertList(projectPlans);
		}
	}
	
	@Override
	public void deleteNursingPlan(Long id) {
		nursingPlanMapper.deleteById(id);
	}
	
	@Override
	public void updateNursingPlanStatus(Long id, Integer status) {
		NursingPlan nursingPlan = new NursingPlan();
		nursingPlan.setId(id);
		nursingPlan.setStatus(status);
		nursingPlanMapper.updateById(nursingPlan);
	}

	@Override
	public NursingPlan getNursingPlanlist() {
		NursingPlan nursingPlan = new NursingPlan();
		nursingPlan.setStatus(1);
		NursingPlan nursingPlanlist = nursingPlanMapper.getNursingPlanlist();
		return  nursingPlanlist;
	}

	@Override
	public void InstallNursingPlan(NursingPlanDto nursingPlanDto) {
		NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
		nursingPlanMapper.InstNursingPlan(nursingPlan);
		//拿到id
		Long nursingPlanId = nursingPlan.getId();
		
		List<NursingProjectPlan> list = nursingPlanDto.getProjectPlans();
		if (CollUtil.isNotEmpty(list)){
			list.forEach(pp -> pp.setPlanId(nursingPlanId));
			nursingProjectPlanMapper.InstsNursingPlan(list);
		}
	}

	@Override
	public PageBean<NursingPlanVo> selectsNationList(Integer pageNum, Integer pageSize, String name, Integer status) {
		PageHelper.startPage(pageNum,pageSize);
		Page<NursingPlanVo> page = nursingProjectPlanMapper.selectsNationList(name,status);
		return PageBean.of(page);
	}
@Transient
	@Override
public NursingPlan cxidNationList(Integer id) {
	// 查询护理计划
	NursingPlan nursingPlan = nursingPlanMapper.cxidNationList(id);
	if (BeanUtil.isEmpty(nursingPlan)) {
		throw new RuntimeException("护理计划不存在，ID: " + id);
	}

	// 查询护理项目计划
	List<NursingProjectPlan> nursingProjectPlan = nursingProjectPlanMapper.cxidNationList(id);

	if (nursingProjectPlan==null) {
		throw new RuntimeException("护理计划没有关联项目，ID: " + id);
	}

	// 转换为NursingPlanVo并设置护理项目计划列表
	NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
	nursingPlanVo.setNursingProjectPlanList(nursingProjectPlan);

	return nursingPlan;
}
@Transient
	@Override
	public void updataNationList(NursingPlanDto nursingPlanDto) {
		NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
		if (BeanUtil.isNotEmpty(nursingPlan)){
			nursingPlanMapper.updataNationList(nursingPlan);
		}
		List<NursingProjectPlan> list = nursingPlanDto.getProjectPlans();
			nursingProjectPlanMapper.updataNationList(list);
	}
	@Transient
	@Override
	public void DeleteNationList(Integer id) {
		nursingPlanMapper.DeleteNationList(id);
		nursingProjectPlanMapper.DeleteNationList(id);
	}

	@Override
	public void updatenursingStatus(Integer id, Integer status) {
		if (status==1||status==0){
			nursingPlanMapper.updatenursingStatus(id,status);
			nursingProjectMapper.getidandstatus(Long.valueOf(id),status);
		}else {
			throw new RuntimeException("状态值不正确");
		}
		
	}
}
